{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "37e51b4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'S2.14=42.05-39.91E'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%run common.ipynb\n",
    "\n",
    "tokenizer.decode(tokenizer.get_data(third_number=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b4ddfae2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 1, 15,  7,  ...,  0,  0,  0],\n",
       "         [ 1, 12,  7,  ..., 13,  2,  0],\n",
       "         [ 1, 15,  6,  ...,  0,  0,  0],\n",
       "         ...,\n",
       "         [ 1, 15, 11,  ...,  0,  0,  0],\n",
       "         [ 1,  9,  7,  ...,  2,  0,  0],\n",
       "         [ 1, 12, 18,  ...,  0,  0,  0]], device='cuda:0'),\n",
       " tensor([[1, 1, 1,  ..., 0, 0, 0],\n",
       "         [1, 1, 1,  ..., 1, 1, 0],\n",
       "         [1, 1, 1,  ..., 0, 0, 0],\n",
       "         ...,\n",
       "         [1, 1, 1,  ..., 0, 0, 0],\n",
       "         [1, 1, 1,  ..., 1, 0, 0],\n",
       "         [1, 1, 1,  ..., 0, 0, 0]], device='cuda:0'))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_batch_data():\n",
    "    data = [tokenizer.get_data(third_number=False) for i in range(64)]\n",
    "\n",
    "    #求最大长度\n",
    "    lens = max([len(i) for i in data])\n",
    "\n",
    "    #做个白板\n",
    "    input_ids = torch.full((len(data), lens),\n",
    "                           tokenizer.encoder['P'],\n",
    "                           device=device)\n",
    "\n",
    "    #往白板里黏贴数据\n",
    "    for i, d in enumerate(data):\n",
    "        input_ids[i, :len(d)] = torch.LongTensor(d)\n",
    "\n",
    "    attention_mask = (input_ids != tokenizer.encoder['P']).long()\n",
    "\n",
    "    return input_ids, attention_mask\n",
    "\n",
    "\n",
    "get_batch_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eb9fe446",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda3/envs/pt2/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 S-0.12=6.38/-54.31EPPPP7E\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 S1.51=73.70/48.89EPPPPP70.35E\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200 S899.02=-55.29*-16.26EP50.1E\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300 S-279.89=-3.43*81.60EPP*-39.6E\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400 S45.21=90.60+-45.39EPPP5E\n",
      "500 S-7218.21=-79.68*90.59E-69.8E\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "600 S161.20=70.67+90.53EPPPE\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "700 S-88.59=-32.78/0.37EPPP-45.49E\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "A decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "800 S18.49=-37.21+55.70EPPPE\n",
      "900 S-167.81=-99.05+-68.76EE\n"
     ]
    }
   ],
   "source": [
    "model_gen = ModelGEN()\n",
    "optimizer = torch.optim.Adam(model_gen.parameters(), lr=1e-4)\n",
    "criterion = torch.nn.CrossEntropyLoss(ignore_index=tokenizer.encoder['P'])\n",
    "\n",
    "for i in range(1000):\n",
    "    input_ids, attention_mask = get_batch_data()\n",
    "\n",
    "    out = model_gen(input_ids=input_ids, attention_mask=attention_mask)\n",
    "\n",
    "    loss = criterion(out[:, :-1].flatten(end_dim=1), input_ids[:,\n",
    "                                                               1:].flatten())\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    if i % 100 == 0:\n",
    "        gen = generate(model_gen, input_ids[0].unsqueeze(0))\n",
    "        print(i, tokenizer.decode(gen[0].tolist()))\n",
    "\n",
    "model_gen.to('cpu')\n",
    "torch.save(model_gen, 'gen.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:pt2]",
   "language": "python",
   "name": "conda-env-pt2-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
